package com.qch.edu.lagou.algorithm.half;

/**
 * @Author qch
 * @Date 2021/7/14
 * 递归二分查找法
 */
public class BinarySearchRecursion {
    /**
     *
     * @param nums 有序数组
     * @param num 数字的最大个数
     * @param n 需要查找的数
     * @return
     */
    public static int binaerSearch(int[] nums,int num,int n)
    {
       return binarySearchUnit(nums,0,num,n);
    }
    public static int binarySearchUnit(int[] nums,int low,int high,int n)
    {
        //没找到
        if(low>high)
            return -1;
        int mid=(low+high)/2;
        if(nums[mid]==n)
        {
            return mid;
        }else if(nums[mid]<n)
        {
            return binarySearchUnit(nums,mid+1,high,n);
        }else {
            return binarySearchUnit(nums,low,mid-1,n);
        }
    }

    public static void main(String[] args) {
        int[] nums={1,3,5,7,9,12,15,18,20};
        System.out.println(binaerSearch(nums,8,20));
    }
}
